﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = listbox1list;
        }
        List<string> listbox1list = new List<string>();
        List<string> bufferlist = new List<string>();
        List<string> bufferstringlist = new List<string>();
        List<int> factors = new List<int>();
        Excel.Application excel = new Excel.Application();
        int bufferint = 0;
        int xaxis = 0;
        int yaxis = 0;
        GlobalInt xaxisglobal = new GlobalInt(0);
        GlobalInt yaxisglobal = new GlobalInt(0);
        bool xed = true;
        string bufferstring = "";
        int bufferintspecial = 0;
        public int times = 1;
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                listbox1list.Add(textBox1.Text);
                reset();
                textBox1.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                listbox1list.RemoveAt(listBox1.SelectedIndex);
                reset();
            }
            catch
            {
                try
                {
                    listbox1list.RemoveAt(listbox1list.Count - 1);
                    reset();
                }
                catch
                {
                }
            }
        }
        private void reset()
        {
            listBox1.DataSource = null;
            listBox1.DataSource = listbox1list;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listbox1list.Clear();
            reset();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (excel == null)
            {
                MessageBox.Show("Excel could not be started! Check that Excel is installed and that all program references are correct.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (listbox1list.Count == 0)
                {
                    return;
                }
                bufferstringlist.Clear();
                //Consider adding dynamic grid size
                factors.Clear();
                factors = PrimeFactors(listbox1list.Count);
                xed = true;
                Form2 form = new Form2(factors, this.xaxis, this.yaxis);
                form.ShowDialog(this);
                for (int g = 1; g <= times; g++)
                {
                    bufferstringlist.Clear();
                    bufferint = 0;
                    bufferstring = "";
                    bufferintspecial = 0;
                    if (xed == true)
                    {
                        return;
                    }
                    //Consider removing visability until later
                    excel.Visible = true;
                    Workbook workbook1 = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                    Worksheet worksheet1 = workbook1.Sheets.Add();
                    worksheet1.Name = "Bingo Chart";
                    worksheet1.StandardWidth = 14;
                    Excel.Range range = worksheet1.Application.get_Range("A1", "F1");
                    bufferint = (int)Math.Floor((double)47 / (double)(xaxis + 1));
                    if (47 - (bufferint * (xaxis + 1)) == xaxis)
                    {
                        bufferint++;
                    }
                    bufferint--;
                    for (int i = 0; i <= bufferint; i++)
                    {
                        try
                        {
                            range = worksheet1.Application.get_Range("A" + (1 + ((xaxis + 1) * i)).ToString(), (Convert.ToChar(64 + yaxis)).ToString() + (xaxis + ((xaxis + 1) * i)).ToString());
                        }
                        catch
                        {
                            bufferstring = "";
                            bufferintspecial = yaxis;
                            for (int b = (int)Math.Ceiling(Math.Log(yaxis, 26)); b > 0; b--)
                            {
                                bufferstring = bufferstring + (Convert.ToChar((int)(64 + (Math.Floor(bufferintspecial / Math.Pow(26, (b - 1))))))).ToString();
                                bufferintspecial = (int)(bufferintspecial - ((int)Math.Floor((bufferintspecial / Math.Pow(26, b - 1)))) * Math.Pow(26, b - 1));
                            }
                            range = worksheet1.Application.get_Range("A" + (1 + ((xaxis + 1) * i)).ToString(), bufferstring + (xaxis + ((xaxis + 1) * i)).ToString());
                        }
                        AllBordersBlack(range);
                    }
                    for (int i = 0; i <= (listbox1list.Count - 1); i++)
                    {
                        bufferlist.Add(listbox1list[i]);
                    }
                    for (int i = 0; i <= (listbox1list.Count - 1); i++)
                    {
                        bufferint = RandomNumber(0, (bufferlist.Count - 1));
                        bufferstringlist.Add(bufferlist[bufferint]);
                        bufferlist.RemoveAt(bufferint);
                    }
                    for (int i = 1; i <= bufferstringlist.Count; i++)
                    {
                        //Insert Data to Excel Document Here, NEEDS FIXING!!!
                        if (Math.Floor((double)i / (double)yaxis) != Math.Ceiling((double)i / (double)yaxis))
                        {
                            bufferint = i - ((int)Math.Floor((double)i / (double)yaxis) * yaxis);
                        }
                        else
                        {
                            bufferint = yaxis;
                        }
                        worksheet1.Cells[(int)Math.Ceiling((double)i / (double)yaxis), bufferint] = bufferstringlist[i - 1];

                    }
                }
            }
        }
        private void AllBordersBlack(Excel.Range range)
        {
            range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Color = Color.Black.ToArgb();
            range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Color = Color.Black.ToArgb();
            range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Color = Color.Black.ToArgb();
            range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Color = Color.Black.ToArgb();
            range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Color = Color.Black.ToArgb();
            range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Color = Color.Black.ToArgb();
        }
        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            try
            {
                return random.Next(min, max);
            }
            catch
            {
                return 0;
            }
        }
        private List<int> PrimeFactors(int num)
        {
            List<int> factors = new List<int>();
            for (int b = 1; b <= num; b++)
            {
                if (num % b == 0)
                {
                    factors.Add(b);
                }
            }
            return factors;
        }
        public void setx(int num)
        {
            xaxis = num;
        }
        public void sety(int num)
        {
            yaxis = num;
        }
        public void setxed(bool value)
        {
            xed = value;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                e.Handled = true;
                button1_Click(this, e);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //Import Alogrithm
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (istextfilename(openFileDialog1.FileName))
                {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName))
                {
                    try
                    {
                        while (sr.Peek() > -1)
                        {
                            listbox1list.Add(sr.ReadLine());
                        }
                    }
                    catch
                    {
                    }
                }
                reset();
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private bool istextfilename(string filename)
        {
            if (filename[filename.Length - 1] == char.Parse("t"))
            {
                if (filename[filename.Length - 2] == char.Parse("x"))
                {
                    if (filename[filename.Length - 3] == char.Parse("t"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //Insert save function here
            //MAKE BUTTON VISIBLE AFTER INSERTING FUNCTION
        }
    }
}
